package com.yty.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yty.dao.StudentMapper;
import com.yty.page.PageResult;
import com.yty.param.StudentQueryParam;
import com.yty.pojo.Student;
import com.yty.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult findStudent(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<Student> students = studentMapper.findStudent(param);
        Page<Student> studentsList = (Page<Student>) students;
        return new PageResult(studentsList.getTotal(),studentsList);
    }

    @Override
    public Integer addStudent(Student student) {
        if (student.getName() == null|| student.getName().equals("") ||
                student.getNo() == null || student.getNo().equals("") ||
                student.getPhone() == null || student.getPhone().equals("") ||
                student.getIdCard() == null || student.getIdCard().equals("") ||
                student.getGender() == null || student.getIsCollege() == null){
            return 0;
        }
        Integer count= studentMapper.addStudent(student);
        return count;
    }

    @Override
    public Integer deleteStudentById(Integer... ids) {
      Integer count = studentMapper.deleteStudentById(ids);
        return count;
    }

    @Override
    public Student findStudentById(Integer id) {
        Student student = studentMapper.findStudentById(id);
        return student;
    }

    @Override
    public Integer updateStudent(Student student) {
        if (student.getName() == null|| student.getName().equals("") ||
                student.getNo() == null || student.getNo().equals("") ||
                student.getPhone() == null || student.getPhone().equals("") ||
                student.getIdCard() == null || student.getIdCard().equals("") ||
                student.getGender() == null || student.getIsCollege() == null){
            return 0;
        }
        Integer count = studentMapper.updateStudent(student);
        return count;
    }

    @Override
    public Integer updateStudentViolation(Integer id, Integer score) {
        if (score <= 0 ){
            return -1;
        }
        Integer count = studentMapper.updateStudentViolation(id,score);
        return count;
    }
}
